package com.hrpmar.controller;

import com.hrpmar.controller.dto.*;
import com.hrpmar.controller.vo.Message;
import com.hrpmar.mapper.DrugstorageMapper;
import com.hrpmar.pojo.*;
import com.hrpmar.service.DruginfoService;
import com.hrpmar.utils.Pager;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by c on 2017-11-15.
 */
@RestController
@RequestMapping("/drug")
public class DrugInfoRestController {
    String mes = null;
    @Resource
    private DruginfoService druginfoService;

    @RequestMapping("/search")
    public Pager searchDruginfo(Integer offset, Integer limit, DruginfoDto condition){
        Pager pager = druginfoService.searchDruginfo(offset,limit,condition);
        return pager;
    }

    @PostMapping("/batchDel")
    public Message batchDel(String[] drugNum){
        Message msg = druginfoService.batchDrugByNum(drugNum);
        return msg;
    }

    @PostMapping("/batchDelAll")
    public Message batchDelAllDrugs(String[] drugNum){
        Message msg = druginfoService.batchAllDrugInfoByNum(drugNum);
        return msg;
    }

    @PostMapping("/getDrugType")
    public List<Drugtype> getDrugType(){
        return druginfoService.getDrugType();
    }

    @PostMapping("/addDrug")
    public String addDrug(Druginfo druginfo){
//        boolean flag;
        try {
            druginfoService.addDrugInfo(druginfo);
//            flag=true;
            mes="药品信息添加成功";
        } catch(RuntimeException e){
//            flag=false;
            mes="系统繁忙,添加失败";
        }
        return mes;
    }

    @PostMapping("/updateDrug")
    public String updateDrug(Druginfo druginfo){
        try{
            druginfoService.updateDtugIngo(druginfo);
            mes="药品信息修改成功!";
        }catch (RuntimeException e){
            mes="系统繁忙,修改失败!";
        }
        return mes;
    }

    @PostMapping("/getPro")
    public Provider getPro(String proNum){
        return druginfoService.getPro(proNum);
    }

    @RequestMapping("/getPurPlan")
    public Pager purPlan(Integer offset, Integer limit, PurplanDto condition){
        return druginfoService.search(offset,limit,condition);
    }

    @PostMapping("/getPurDels")
    public List<Purdetails> getPurDels(String planNum){
        List<Purdetails> list = new ArrayList<Purdetails>();
        list = druginfoService.searchDetails(planNum);
        return list;
    }

    @PostMapping("/toMap")
    public String toMap(String planNum, HttpServletRequest req){
        String flag = "false";
        try{
            druginfoService.addDsdels(planNum,req);
            flag="true";
        }catch (RuntimeException e){
            flag="false";
        }
        return flag;
    }

    /**
     * 获取最新入库单据编号
     * @return
     */
    @PostMapping("/getDeNum")
    public String getNewDeNum(){
        return druginfoService.getNewDeNum();
    }

    @PostMapping("/editdedels")
    public dsdDto editDeDels(String drugNum, HttpServletRequest req){
        HttpSession session = req.getSession();
        Map<String,dsdDto> map=(Map<String,dsdDto>)session.getAttribute("dsdDelsMap");
        dsdDto d = map.get(drugNum);
        return d;
    }

    @PostMapping("/upDedels")
    public String upDedels(String drugNum,String dsdLotnum,String stoMfg,String dsdValiddate,HttpServletRequest req){
        String flag = "false";
        try{
            //获取session里map然后修改
            HttpSession session = req.getSession();
            Map<String,dsdDto> map=(Map<String,dsdDto>)session.getAttribute("dsdDelsMap");
            dsdDto d = map.get(drugNum);
            SimpleDateFormat sfm = new SimpleDateFormat("yyyy-MM-dd");
            d.setDsdLotnum(dsdLotnum);
            try {
                d.setStoMfg(sfm.parse(stoMfg));
                d.setDsdValiddate(sfm.parse(dsdValiddate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            flag="true";
        }catch (RuntimeException e){
            flag="false";
        }
        return flag;
    }

    @PostMapping("/ruku")
    public String addRuKu(String proNum,String planNum,String dsNum,String planDate,String dsVerifier,HttpServletRequest req){
        String flag = "false";
        try{
            druginfoService.addRukuInfo(proNum,planNum,dsNum,planDate,dsVerifier,req);
            flag="true";
        }catch (RuntimeException e){
            flag="false";
        }
        return flag;
    }

    @RequestMapping("/searchRu")
    public Pager searchRu(Integer offset, Integer limit, DrugstorageDto condition){
        return druginfoService.searchRu(offset,limit,condition);
    }

    @RequestMapping("/searchChu")
    public Pager searchChu(Integer offset, Integer limit, OutstorageDto condition){
        return druginfoService.searchChu(offset,limit,condition);
    }

    @RequestMapping("/getmedicper")
    public List<Medicper> searchAllMed(){
        List<Medicper> med = druginfoService.getAllMed();
        return med;
    }

    @PostMapping("/getPriById")
    public Medicper getPriById(Integer perId){
        Medicper medicper = druginfoService.getMedById(perId);
        return medicper;
    }

    @PostMapping("/getOutstorageNum")
    public String getOutstorageNum(){
        return druginfoService.getNewOutNum();
    }

    @PostMapping("/getDrugSto")
    public List<Drugstock> getDrugSto(String drugNum){
        List<Drugstock> list = druginfoService.searchStoByDrugNum(drugNum);
        return list;
    }

    @PostMapping("/getStoBystoLotnum")
    public Drugstock getStoBystoLotnum(String osdLotnum,String drugNum){
        Drugstock drugstock = druginfoService.getStoBystoLotnum(osdLotnum,drugNum);
        return drugstock;
    }

    @PostMapping("/addOutDelsInMap")
    public Map<Integer,OsdetailsDto> addOutDelsInMap(OsdetailsDto osd,HttpServletRequest req){
        Map<Integer,OsdetailsDto> map = druginfoService.setOutDelsInMap(osd,req);
        return map;
    }

    @PostMapping("/delOutDels")
    public String addOutDelsInMap(Integer keyId,HttpServletRequest req){
        String flag = "false";
        try{
            druginfoService.delOutDels(keyId,req);
            flag="true";
        }catch (RuntimeException e){
            flag = "false";
        }
        return flag;
    }

    @PostMapping("/updateOutDelsInMap")
    public String updateOutDelsInMap(Integer osdNum,Integer keyId,HttpServletRequest req){
        String flag = "false";
        try{
            druginfoService.updateOutDels(osdNum,keyId,req);
            flag="true";
        }catch (RuntimeException e){
            flag = "false";
        }
        return flag;
    }

    @PostMapping("/addOutDelsAndOutSto")
    public String addOutDelsAndOutSto(Outstorage outstorage,HttpServletRequest req){
        String flag="false";
        try{
            druginfoService.addOutstoAndDsdeltails(outstorage,req);
            flag="true";
        }catch (RuntimeException e){
            flag="false";
        }
        return flag;
    }



}
